package com.ghost.manager
{
	import com.ghost.UI.Ghost_tip;
	import com.ghost.model.Object_pool;
	import com.ghost.model.UI_skin_data;
	import com.ghost.utils.GC;
	import com.ghost.view.tip.Tip0;
	import com.ghost.view.tip.Tip1;
	import com.ghost.view.tip.Tip2;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.MouseEvent;

	/**
	 * @author ghost
	 * tip管理器 
	 */	
	public class Tip_manager
	{
		private static var instance:Tip_manager;
		public static function getInstance():Tip_manager
		{
			if(instance == null)
			{
				instance = new Tip_manager();
			}
			return instance;
		}
		private var tip:Ghost_tip;
		private var obj_mc:DisplayObject;
		private var target_obj:*;
		private var target_tip:Ghost_tip;
		public function Tip_manager()
		{
			UI_skin_data.ghost_tip_model_v = new Vector.<Object>();
			UI_skin_data.ghost_tip_model_v.push(Tip0);
			UI_skin_data.ghost_tip_model_v.push(Tip1);
			UI_skin_data.ghost_tip_model_v.push(Tip2);
		}
		/**
		 * @param obj 目标对象
		 * @param id 目标id 绑定到目标的name
		 * @param tip_type 目标tip类型（-1:默认的只提供文本,0:,1:,....）
		 * 
		 */		
		public function add_tip(obj:DisplayObject,id:String,source_xml:String="static_tip_xml",tip_type:int=0,color_n:int=0):void
		{
			if(obj==null || id=="")return;
			if(!Object_pool.tip_obj_pool.containsKey(obj))
			{
				tip = new Ghost_tip(obj,source_xml,id,tip_type,color_n);
				Object_pool.tip_obj_pool.put(obj,tip);
			}
			obj_mc = obj;
			obj.addEventListener(MouseEvent.MOUSE_OVER,over_Handler);
			obj.addEventListener(MouseEvent.MOUSE_OUT,out_Handler);
		}
		private function over_Handler(e:MouseEvent):void
		{
			target_obj = e.currentTarget;
			target_tip = Object_pool.tip_obj_pool.get(target_obj);
			if(target_obj.stage != null)
			{
				target_obj.stage.addChild(target_tip)
			}
		}
		private function out_Handler(e:MouseEvent):void
		{
			target_obj = e.currentTarget;
			target_tip = Object_pool.tip_obj_pool.get(target_obj);
			if(target_obj.stage != null && (target_obj.stage.contains(target_tip)))
			{
				target_obj.stage.removeChild(target_tip);
				target_tip = null;
				GC.nowGC();
			}
		}
		/**
		 * @param obj 解除tip绑定
		 */		
		public function remove_tip(obj:DisplayObject):void
		{
			target_tip = Object_pool.tip_obj_pool.get(obj);
			if(obj.stage != null && (obj.stage.contains(target_tip)))
			{
				obj.stage.removeChild(target_tip);
				target_tip = null;
				GC.nowGC();
			}
			Object_pool.tip_obj_pool.delete_key_content(obj);//从对象池里删除并且从内存中删除
			obj.removeEventListener(MouseEvent.MOUSE_OVER,over_Handler);
			obj.removeEventListener(MouseEvent.MOUSE_OUT,out_Handler);
		}
	}
}